﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TryItPages
{

    public partial class _Default : System.Web.UI.Page
    {
        private SavedSearchesService.SavedSearchesServiceClient client = new SavedSearchesService.SavedSearchesServiceClient();

        private void AddErrorText(bool isError, StringBuilder builder)
        {
            if (isError)
            {
                builder.Append("<div class=\"alert alert-error\">");
                builder.Append("<button type=\"button\" class=\"close\" data-dismiss=\"alert\">&times;</button>");
                builder.Append("<strong>Error:</strong> Error occurred invoking the service.");
                builder.Append("</div>");
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            String requestMethod = Request.QueryString["serviceMethod"];
            String user = Request.QueryString["user"];
            String query = Request.QueryString["query"];
            StringBuilder builder = new StringBuilder();
            bool isError = false;

            if ("GetAllUsers".Equals(requestMethod))
            {
                string[] results = null;
                try
                {
                    results = this.client.GetAllUsers();
                }
                catch (Exception ex)
                {
                    isError = true;
                    results = new string[]{ex.Message};
                }
                this.AddErrorText(isError, builder);
                builder.Append("<table class=\"table\">");
                builder.Append("<tr><th>GetAllUsers(Input)</th></tr>");
                foreach (String userName in results)
                {
                    builder.Append("<tr><td>" + userName + "</td></tr>");
                }
                builder.Append("</table>");
            }
            else if ("SaveUserSearch".Equals(requestMethod))
            {
                string results = null;
                try
                {
                    results = this.client.SaveUserSearch(user, query).ToString();
                }
                catch (Exception ex)
                {
                    isError = true;
                    results = ex.Message;
                }
                this.AddErrorText(isError, builder);

                builder.Append("<table class=\"table\">");
                builder.Append("<tr><th>Input</th><th>SaveUserSearch(Input)</th></tr>");
                builder.Append("<tr><td>" + user + ", " + query + "</td><td>" + results + "</td></tr>");
                builder.Append("</table>");
            }
            else if ("GetSearchesForUser".Equals(requestMethod))
            {
                string[] results = null;
                try
                {
                    results = this.client.GetSearchesForUser(user);
                }
                catch (Exception ex)
                {
                    isError = true;
                    results = new string[] { ex.Message };
                }
                this.AddErrorText(isError, builder);

                builder.Append("<table class=\"table\">");
                builder.Append("<tr><th>Input</th><th>GetSearchesForUser(Input)</th></tr>");
                foreach (String search in results)
                {
                    builder.Append("<tr><td>" + user + "</td><td>" + search + "</td></tr>");
                }
                builder.Append("</table>");
            }


            if (requestMethod != null) {
                ResultContent.Text = builder.ToString();
            }
         
        }
    }
}
